package com.xingxue.shopping.model.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.xingxue.shopping.model.dao.ITypeDao;
import com.xingxue.shopping.model.persist.Goods;
import com.xingxue.shopping.model.persist.Type;

public class TypeDaoImpl extends BaseDao implements ITypeDao {

	@Override
	public List<Type> getAll() {
		// TODO Auto-generated method stub
				List<Type>list=new ArrayList<Type>();
				String Sql="SELECT * FROM type";
				Connection connection=getConnection();
				PreparedStatement pStatement=null;
				ResultSet liSet=null;
				
				try {
					pStatement=connection.prepareStatement(Sql);
					liSet=pStatement.executeQuery();
					
					
					while (liSet.next())
					{
						Type type=new Type();
						type.setTypeid(liSet.getInt("typeid"));;
						type.setTypename(liSet.getString("typename"));
						
						
						list.add(type);
					
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}finally {
					try {
						pStatement.close();
						liSet.close();
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				
				
				return list;
	}

	@Override
	public Integer addType(Type t) {
		// TODO Auto-generated method stub
		String sql="INSERT INTO `type` (`typeid`, `typename`) VALUES (?, ?);";
		 Connection connection=getConnection();
		 PreparedStatement pStatement=null;
	     try {
			pStatement=connection.prepareStatement(sql);
			pStatement.setInt(1, t.getTypeid());
			pStatement.setString(2,t.getTypename());
			
			
			int result=pStatement.executeUpdate();
			return result;
			
			
			
			
			
			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 0;
		}finally {
			try {
				pStatement.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
